package com.imnc.foodmap.ui;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.search.MKAddrInfo;
import com.baidu.mapapi.search.MKBusLineResult;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPoiInfo;
import com.baidu.mapapi.search.MKPoiResult;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKSearchListener;
import com.baidu.mapapi.search.MKShareUrlResult;
import com.baidu.mapapi.search.MKSuggestionResult;
import com.baidu.mapapi.search.MKTransitRouteResult;
import com.baidu.mapapi.search.MKWalkingRouteResult;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.imnc.foodmap.BaseApplication;
import com.imnc.foodmap.R;
import com.imnc.foodmap.model.LocationDataParcelable;
import com.imnc.foodmap.model.PoiInfoParcelable;
import com.imnc.foodmap.ui.adapter.SearchResultAdapter;
import com.imnc.foodmap.ui.base.BaseSlidingActivity;

public class MainActivity extends BaseSlidingActivity {

	public static final String MAP_ALL_POI_RESULT = "MAP_ALL_POI_RESULT";
	public static final String MAP_LOCATION_DATA = "MAP_LOCATION_DATA";

	/** 搜索 **/
	private MKSearch mSearch = null;
	private MKSearchListener mSearchListener = null;

	/** 定位 **/
	private LocationClient mLocationClient = null;
	private BDLocationListener mLocationListener = null;
	public static LocationData locationData = null;

	/** UI **/
	private ListView mResultListView = null;
	private SearchResultAdapter mAdapter = null;
	public static ArrayList<MKPoiInfo> searchResultInfo = null;

	private boolean isLocation = false;
	private String title = "美食";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setSlidingActionBarEnabled(true);
		setContentView(R.layout.activity_main);

		// 初始化map
		if (mApplication.getmBMapManager() == null) {
			mApplication.setmBMapManager(new BMapManager(
					getApplicationContext()));
			mApplication.getmBMapManager().init(
					new BaseApplication.EatMapMKGeneralListener());
		}

		initialize();
		initEvents();
	}

	private void initialize() {
		mSearch = new MKSearch();
		mSearchListener = new SearchListener();

		mLocationClient = new LocationClient(getApplicationContext());
		mLocationListener = new MyLocationListener();
		locationData = new LocationData();

		mResultListView = (ListView) findViewById(R.id.lv_search_result);
		mAdapter = new SearchResultAdapter(this);
		searchResultInfo = new ArrayList<MKPoiInfo>();
	}

	private void initEvents() {
		mSearch.init(mApplication.getmBMapManager(), mSearchListener);

		mResultListView.setAdapter(mAdapter);

		mLocationClient.registerLocationListener(mLocationListener);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setAddrType("all");// 返回的定位结果包含地址信息
		option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
		option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
		option.disableCache(true);// 禁止启用缓存定位
		option.setPoiNumber(5); // 最多返回POI个数
		option.setPoiDistance(1000); // poi查询距离
		option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息
		mLocationClient.setLocOption(option);
		mLocationClient.start();

		mResultListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parnet, View view,
					int position, long id) {

				Bundle bundle = new Bundle();
				bundle.putParcelable(MainActivity.MAP_ALL_POI_RESULT,
						new PoiInfoParcelable(searchResultInfo.get(position)));
				startActivity(PoiResultDetailActivity.class, bundle);

			}
		});
	}

	@Override
	protected void onDestroy() {
		mSearch.destory();
		mLocationClient.start();

		super.onDestroy();
	}

	public void switchMap(String content) {

		if (!isLocation) {
			return;
		}
		title = content;
		mSearch.poiSearchNearBy(content, new GeoPoint(
				(int) (locationData.latitude * 1e6),
				(int) (locationData.longitude * 1e6)), 2000);
		getSlidingMenu().showContent();
	}

	// actionBar
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getSupportMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.detail:

			if (!isLocation) {
				break;
			}
			Bundle bundle = new Bundle();
			bundle.putParcelable(MAP_LOCATION_DATA, new LocationDataParcelable(
					locationData));
			ArrayList<PoiInfoParcelable> allPoiInfo = new ArrayList<PoiInfoParcelable>();
			for (MKPoiInfo poiInfo : searchResultInfo) {
				allPoiInfo.add(new PoiInfoParcelable(poiInfo));
			}

			bundle.putParcelableArrayList(MAP_ALL_POI_RESULT, allPoiInfo);
			startActivity(MapActivity.class, bundle);
			break;
		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	// 内部类
	private class MyLocationListener implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {

			if (location == null) {
				return;
			}

			locationData.latitude = location.getLatitude();
			locationData.longitude = location.getLongitude();
			locationData.accuracy = location.getRadius();
			locationData.direction = location.getDerect();

			isLocation = true;
			switchMap(title);
		}

		@Override
		public void onReceivePoi(BDLocation location) {

		}

	}

	private class SearchListener implements MKSearchListener {

		public void onGetAddrResult(MKAddrInfo info, int iError) {
		}

		public void onGetBusDetailResult(MKBusLineResult result, int iError) {
		}

		public void onGetDrivingRouteResult(MKDrivingRouteResult res, int iError) {
		}

		@Override
		public void onGetPoiDetailSearchResult(int type, int iError) {
		}

		@Override
		public void onGetPoiResult(MKPoiResult result, int type, int iError) {
			if (iError != 0 || result == null) {
				return;
			}
			if (result.getCurrentNumPois() > 0) {
				searchResultInfo = result.getAllPoi();
				mAdapter.setItemContent(searchResultInfo);
				mAdapter.notifyDataSetChanged();
			}

		}

		@Override
		public void onGetShareUrlResult(MKShareUrlResult result, int type,
				int iError) {

		}

		@Override
		public void onGetSuggestionResult(MKSuggestionResult result, int iError) {

		}

		@Override
		public void onGetTransitRouteResult(MKTransitRouteResult result,
				int iError) {

		}

		@Override
		public void onGetWalkingRouteResult(MKWalkingRouteResult result,
				int iError) {

		}

	}

}
